<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备名称" prop="dvName">
        <el-input
          v-model="queryParams.dvName"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备编码" prop="dvCode">
        <el-input
          v-model="queryParams.dvCode"
          placeholder="请输入设备编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="购置价格" prop="dvPrice">
        <el-input
          v-model="queryParams.dvPrice"
          placeholder="请输入购置价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备数量" prop="dvNum">
        <el-input
          v-model="queryParams.dvNum"
          placeholder="请输入设备数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备类型" prop="dvType">
        <el-select v-model="queryParams.dvType" placeholder="请选择设备类型" clearable>
          <el-option
            v-for="dict in dict.type.dv_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="设备型号" prop="dvTypeCode">
        <el-input
          v-model="queryParams.dvTypeCode"
          placeholder="请输入设备型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="区域名字" prop="addressName">
        <el-select v-model="queryParams.addressName" placeholder="请选择区域名字" clearable>
          <el-option
            v-for="dict in dict.type.address_name"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="资产编号" prop="zcCode">
        <el-input
          v-model="queryParams.zcCode"
          placeholder="请输入资产编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单位标识" prop="source">
        <el-input
          v-model="queryParams.source"
          placeholder="请输入单位标识"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="耐用年限" prop="durableYear">
        <el-input
          v-model="queryParams.durableYear"
          placeholder="请输入耐用年限"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产时间" prop="productDate">
        <el-input
          v-model="queryParams.productDate"
          placeholder="请输入生产时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="使用年限" prop="useYear">
        <el-input
          v-model="queryParams.useYear"
          placeholder="请输入使用年限"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出产国别" prop="producingCountry">
        <el-input
          v-model="queryParams.producingCountry"
          placeholder="请输入出产国别"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备品牌" prop="dvModel">
        <el-input
          v-model="queryParams.dvModel"
          placeholder="请输入设备品牌"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="启用日期" prop="startDate">
        <el-input
          v-model="queryParams.startDate"
          placeholder="请输入启用日期"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备净值" prop="dvPurePrice">
        <el-input
          v-model="queryParams.dvPurePrice"
          placeholder="请输入设备净值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="使用部门" prop="useDept">
        <el-input
          v-model="queryParams.useDept"
          placeholder="请输入使用部门"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="使用人员" prop="usePeople">
        <el-input
          v-model="queryParams.usePeople"
          placeholder="请输入使用人员"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出产时间" prop="factoryDate">
        <el-date-picker clearable
          v-model="queryParams.factoryDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择出产时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="安装地点" prop="buildAddress">
        <el-input
          v-model="queryParams.buildAddress"
          placeholder="请输入安装地点"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备供应商" prop="dvSupplier">
        <el-input
          v-model="queryParams.dvSupplier"
          placeholder="请输入设备供应商"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计量单位" prop="dvNumUnit">
        <el-input
          v-model="queryParams.dvNumUnit"
          placeholder="请输入计量单位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备原值" prop="dvOriPrice">
        <el-input
          v-model="queryParams.dvOriPrice"
          placeholder="请输入设备原值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备折旧" prop="dvDepre">
        <el-input
          v-model="queryParams.dvDepre"
          placeholder="请输入设备折旧"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备折旧率" prop="dvDepreRate">
        <el-input
          v-model="queryParams.dvDepreRate"
          placeholder="请输入设备折旧率"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input
          v-model="queryParams.sort"
          placeholder="请输入排序"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['base:device:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['base:device:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['base:device:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['base:device:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="deviceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['base:device:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['base:device:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDowloadBarCode(scope.row)"
            v-hasPermi="['base:device:remove']"
          >下载二维码</el-button>
        </template>
      </el-table-column>
      <!-- <el-table-column label="id
" align="center" prop="id" /> -->
      <el-table-column label="设备名称" align="center" prop="dvName" />
      <el-table-column label="设备编码" align="center" prop="dvCode" />
      <el-table-column label="型号规格" align="center" prop="cvType" />
      <el-table-column label="购置价格" align="center" prop="dvPrice" />
      <el-table-column label="设备描述" align="center" prop="dvDescribe" />
      <el-table-column label="设备数量" align="center" prop="dvNum" />
      <el-table-column label="设备类型" align="center" prop="dvType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.dv_type" :value="scope.row.dvType"/>
        </template>
      </el-table-column>
      <el-table-column label="设备型号" align="center" prop="dvTypeCode" />
      <el-table-column label="区域名字" align="center" prop="addressName">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.address_name" :value="scope.row.addressName"/>
        </template>
      </el-table-column>
      <el-table-column label="资产编号" align="center" prop="zcCode" />
      <el-table-column label="单位标识" align="center" prop="source" />
      <el-table-column label="耐用年限" align="center" prop="durableYear" />
      <el-table-column label="生产时间" align="center" prop="productDate" />
      <el-table-column label="使用年限" align="center" prop="useYear" />
      <el-table-column label="出产国别" align="center" prop="producingCountry" />
      <el-table-column label="设备品牌" align="center" prop="dvModel" />
      <el-table-column label="启用日期" align="center" prop="startDate" />
      <el-table-column label="设备净值" align="center" prop="dvPurePrice" />
      <el-table-column label="使用部门" align="center" prop="useDept" />
      <el-table-column label="使用人员" align="center" prop="usePeople" />
      <el-table-column label="出产时间" align="center" prop="factoryDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.factoryDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="安装地点" align="center" prop="buildAddress" />
      <el-table-column label="设备供应商" align="center" prop="dvSupplier" />
      <el-table-column label="计量单位" align="center" prop="dvNumUnit" />
      <el-table-column label="设备原值" align="center" prop="dvOriPrice" />
      <el-table-column label="设备折旧" align="center" prop="dvDepre" />
      <el-table-column label="设备折旧率" align="center" prop="dvDepreRate" />
      <el-table-column label="附件地址" align="center" prop="fjUrl" />
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="备注" align="center" prop="remark" />
    
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="设备名称" prop="dvName">
          <el-input v-model="form.dvName" placeholder="请输入设备名称" />
        </el-form-item>
        <el-form-item label="设备编码" prop="dvCode">
          <el-input v-model="form.dvCode" placeholder="请输入设备编码" />
        </el-form-item>
        <el-form-item label="购置价格" prop="dvPrice">
          <el-input v-model="form.dvPrice" placeholder="请输入购置价格" />
        </el-form-item>
        <el-form-item label="设备描述" prop="dvDescribe">
          <el-input v-model="form.dvDescribe" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="设备数量" prop="dvNum">
          <el-input v-model="form.dvNum" placeholder="请输入设备数量" />
        </el-form-item>
        <el-form-item label="设备类型" prop="dvType">
          <el-select v-model="form.dvType" placeholder="请选择设备类型">
            <el-option
              v-for="dict in dict.type.dv_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备型号" prop="dvTypeCode">
          <el-input v-model="form.dvTypeCode" placeholder="请输入设备型号" />
        </el-form-item>
        <el-form-item label="区域名字" prop="addressName">
          <el-select v-model="form.addressName" placeholder="请选择区域名字">
            <el-option
              v-for="dict in dict.type.address_name"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="资产编号" prop="zcCode">
          <el-input v-model="form.zcCode" placeholder="请输入资产编号" />
        </el-form-item>
        <el-form-item label="单位标识" prop="source">
          <el-input v-model="form.source" placeholder="请输入单位标识" />
        </el-form-item>
        <el-form-item label="耐用年限" prop="durableYear">
          <el-input v-model="form.durableYear" placeholder="请输入耐用年限" />
        </el-form-item>
        <el-form-item label="生产时间" prop="productDate">
          <el-input v-model="form.productDate" placeholder="请输入生产时间" />
        </el-form-item>
        <el-form-item label="使用年限" prop="useYear">
          <el-input v-model="form.useYear" placeholder="请输入使用年限" />
        </el-form-item>
        <el-form-item label="出产国别" prop="producingCountry">
          <el-input v-model="form.producingCountry" placeholder="请输入出产国别" />
        </el-form-item>
        <el-form-item label="设备品牌" prop="dvModel">
          <el-input v-model="form.dvModel" placeholder="请输入设备品牌" />
        </el-form-item>
        <el-form-item label="启用日期" prop="startDate">
          <el-input v-model="form.startDate" placeholder="请输入启用日期" />
        </el-form-item>
        <el-form-item label="设备净值" prop="dvPurePrice">
          <el-input v-model="form.dvPurePrice" placeholder="请输入设备净值" />
        </el-form-item>
        <el-form-item label="使用部门" prop="useDept">
          <el-input v-model="form.useDept" placeholder="请输入使用部门" />
        </el-form-item>
        <el-form-item label="使用人员" prop="usePeople">
          <el-input v-model="form.usePeople" placeholder="请输入使用人员" />
        </el-form-item>
        <el-form-item label="出产时间" prop="factoryDate">
          <el-date-picker clearable
            v-model="form.factoryDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择出产时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="安装地点" prop="buildAddress">
          <el-input v-model="form.buildAddress" placeholder="请输入安装地点" />
        </el-form-item>
        <el-form-item label="设备供应商" prop="dvSupplier">
          <el-input v-model="form.dvSupplier" placeholder="请输入设备供应商" />
        </el-form-item>
        <el-form-item label="计量单位" prop="dvNumUnit">
          <el-input v-model="form.dvNumUnit" placeholder="请输入计量单位" />
        </el-form-item>
        <el-form-item label="设备原值" prop="dvOriPrice">
          <el-input v-model="form.dvOriPrice" placeholder="请输入设备原值" />
        </el-form-item>
        <el-form-item label="设备折旧" prop="dvDepre">
          <el-input v-model="form.dvDepre" placeholder="请输入设备折旧" />
        </el-form-item>
        <el-form-item label="设备折旧率" prop="dvDepreRate">
          <el-input v-model="form.dvDepreRate" placeholder="请输入设备折旧率" />
        </el-form-item>
        <el-form-item label="附件地址" prop="fjUrl">
          <file-upload v-model="form.fjUrl"/>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-divider content-position="center">报修管理信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddTbRepair">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteTbRepair">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="tbRepairList" :row-class-name="rowTbRepairIndex" @selection-change="handleTbRepairSelectionChange" ref="tbRepair">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="故障日期" prop="troubleDate" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.troubleDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择故障日期" />
            </template>
          </el-table-column>
          <el-table-column label="设备名称" prop="dvName" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.dvName" placeholder="请输入设备名称" />
            </template>
          </el-table-column>
          <el-table-column label="设备编码" prop="dvCode" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.dvCode" placeholder="请输入设备编码" />
            </template>
          </el-table-column>
          <el-table-column label="检修负责人" prop="overhaulPerson" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.overhaulPerson" placeholder="请输入检修负责人" />
            </template>
          </el-table-column>
          <el-table-column label="型号规格" prop="cvType" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.cvType" placeholder="请选择型号规格">
                <el-option label="请选择字典生成" value="" />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="报修时间" prop="repairsDate" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.repairsDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择报修时间" />
            </template>
          </el-table-column>
          <el-table-column label="使用部门" prop="usePart" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.usePart" placeholder="请输入使用部门" />
            </template>
          </el-table-column>
          <el-table-column label="维修人" prop="fixPerson" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.fixPerson" placeholder="请输入维修人" />
            </template>
          </el-table-column>
          <el-table-column label="修理时间" prop="fixDate" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.fixDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择修理时间" />
            </template>
          </el-table-column>
          <el-table-column label="修理完工时间" prop="fixEndDate" width="240">
            <template slot-scope="scope">
              <el-date-picker clearable v-model="scope.row.fixEndDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择修理完工时间" />
            </template>
          </el-table-column>
          <el-table-column label="故障部位" prop="troubleBw" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.troubleBw" placeholder="请输入故障部位" />
            </template>
          </el-table-column>
          <el-table-column label="配件更换" prop="pjChange" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.pjChange" placeholder="请输入配件更换" />
            </template>
          </el-table-column>
          <el-table-column label="排序" prop="sort" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.sort" placeholder="请输入排序" />
            </template>
          </el-table-column>
          <el-table-column label="备注" prop="remark" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.remark" placeholder="请输入备注" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDevice,getBarCode, getDevice, delDevice, addDevice, updateDevice } from "@/api/base/device";

export default {
  name: "Device",
  dicts: ['address_name', 'dv_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedTbRepair: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备管理表格数据
      deviceList: [],
      // 报修管理表格数据
      tbRepairList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dvName: null,
        dvCode: null,
        cvType: null,
        dvPrice: null,
        dvDescribe: null,
        dvNum: null,
        dvType: null,
        dvTypeCode: null,
        addressName: null,
        zcCode: null,
        source: null,
        durableYear: null,
        productDate: null,
        useYear: null,
        producingCountry: null,
        dvModel: null,
        startDate: null,
        dvPurePrice: null,
        useDept: null,
        usePeople: null,
        factoryDate: null,
        buildAddress: null,
        dvSupplier: null,
        dvNumUnit: null,
        dvOriPrice: null,
        dvDepre: null,
        dvDepreRate: null,
        fjUrl: null,
        sort: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        dvName: [
          { required: true, message: "设备名称不能为空", trigger: "blur" }
        ],
        dvCode: [
          { required: true, message: "设备编码不能为空", trigger: "blur" }
        ],
        cvType: [
          { required: true, message: "型号规格不能为空", trigger: "change" }
        ],
        dvPrice: [
          { required: true, message: "购置价格不能为空", trigger: "blur" }
        ],
        dvNum: [
          { required: true, message: "设备数量不能为空", trigger: "blur" }
        ],
        source: [
          { required: true, message: "单位标识不能为空", trigger: "blur" }
        ],
        durableYear: [
          { required: true, message: "耐用年限不能为空", trigger: "blur" }
        ],
        dvNumUnit: [
          { required: true, message: "计量单位不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询设备管理列表 */
    getList() {
      this.loading = true;
      listDevice(this.queryParams).then(response => {
        this.deviceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    async  downloadImg(url) {
      try {
        const res = await fetch(url);
        if (!res.ok) {
          throw new Error("fetch network response was not ok");
        }
        const blob = await res.blob();
        if (
          blob.type.includes("image") ||
          blob.type.includes("audio") ||
          blob.type.includes("video")
        ) {
          const a = document.createElement("a");
          a.href = URL.createObjectURL(blob);
          a.download = "";
          document.body.appendChild(a);
          a.click();
        } else {
          window.open(url);
        }
      } catch (error) {
        //有些图片url请求本身就出现了跨域等问题，目前纯前端本人还无解，只能直接open
        console.log("catcherror", err);
        window.open(url);
      }
    },
   async handleDowloadBarCode(row){
      // 下载条形码
      // 获取图片并下载
   const {msg}= await getBarCode(row.dvCode)
   this.downloadImg(msg)
   
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        updateTime: null,
        updateBy: null,
        createBy: null,
        createTime: null,
        dvName: null,
        dvCode: null,
        cvType: null,
        dvPrice: null,
        dvDescribe: null,
        dvNum: null,
        dvType: null,
        dvTypeCode: null,
        addressName: null,
        zcCode: null,
        source: null,
        durableYear: null,
        productDate: null,
        useYear: null,
        producingCountry: null,
        dvModel: null,
        startDate: null,
        dvPurePrice: null,
        useDept: null,
        usePeople: null,
        factoryDate: null,
        buildAddress: null,
        dvSupplier: null,
        dvNumUnit: null,
        dvOriPrice: null,
        dvDepre: null,
        dvDepreRate: null,
        fjUrl: null,
        sort: null,
        remark: null
      };
      this.tbRepairList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDevice(id).then(response => {
        this.form = response.data;
        this.tbRepairList = response.data.tbRepairList;
        this.open = true;
        this.title = "修改设备管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.tbRepairList = this.tbRepairList;
          if (this.form.id != null) {
            updateDevice(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDevice(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除设备管理编号为"' + ids + '"的数据项？').then(function() {
        return delDevice(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 报修管理序号 */
    rowTbRepairIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 报修管理添加按钮操作 */
    handleAddTbRepair() {
      let obj = {};
      obj.troubleDate = "";
      obj.dvName = "";
      obj.dvCode = "";
      obj.overhaulPerson = "";
      obj.cvType = "";
      obj.repairsDate = "";
      obj.usePart = "";
      obj.fixPerson = "";
      obj.fixDate = "";
      obj.fixEndDate = "";
      obj.troubleImg = "";
      obj.troubleXh = "";
      obj.troubleWhy = "";
      obj.troubleBw = "";
      obj.pjChange = "";
      obj.troubleResult = "";
      obj.completeImg = "";
      obj.sort = "";
      obj.remark = "";
      this.tbRepairList.push(obj);
    },
    /** 报修管理删除按钮操作 */
    handleDeleteTbRepair() {
      if (this.checkedTbRepair.length == 0) {
        this.$modal.msgError("请先选择要删除的报修管理数据");
      } else {
        const tbRepairList = this.tbRepairList;
        const checkedTbRepair = this.checkedTbRepair;
        this.tbRepairList = tbRepairList.filter(function(item) {
          return checkedTbRepair.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleTbRepairSelectionChange(selection) {
      this.checkedTbRepair = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('base/device/export', {
        ...this.queryParams
      }, `device_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
